package com.singfan.common.util;

import com.singfan.common.SingFanException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件工具类
 * Author: HanSongHe
 * Date: 2015-07-27 14:27
 * Desc:
 */
public class FileUtil {
    /**
     * 读取每行为一个单位存储的文本内容
     * @param file 文本文件
     * @return 文本文件内容
     */
    public static List<String> readLineText(File file){
        List<String> result = new ArrayList<>();

        if(file != null && file.exists()){
            try {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String tempString;
                while ((tempString = reader.readLine()) != null) {
                    result.add(tempString);
                }
                reader.close();
            }
            catch (Exception ex){
                throw new SingFanException(String.format("FileUtil.readLineText(file:%s) error: %s !", file.getName(), ex.getMessage()), ex);
            }
        }
        else {
            if (file == null){
                throw new SingFanException("FileUtil.readLineText() error: file is null !");
            }
            else {
                throw new SingFanException("FileUtil.readLineText() error: file is not exist !");
            }
        }

        return result;
    }
}
